package com.chance.cc.crawler.development.command.schedule.xhs;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.development.command.meta.keyword.CrawlerDomainKeywordCommand;
import com.chance.cc.crawler.meta.core.bean.crawler.CrawlerDomainKeys;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.commons.io.IOUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bx
 * @date 2020/12/15 0015 13:26
 */
@Data
@AllArgsConstructor
public class XHSKeywordsImport {

    private String keyword;

    private String category;

//    private String businessTag;

    public static void main(String[] args) throws IOException {


//        processKW();
//        processKW1();

        localProfile();//添加关键词
//        testProfile();//添加用户
    }

    private static void testProfile() throws IOException {
        List<String> kwLines = IOUtils.readLines(new FileInputStream("D:\\chance\\gitspace\\chance-crawler-development\\crawler-scripts-command\\src\\main\\java\\com\\chance\\cc\\crawler\\development\\command\\schedule\\xhs\\xhs_kw.txt"), Charset.forName("UTF-8"));

        for (String kwLine : kwLines) {
            if (kwLine.startsWith("#")) {
                continue;
            }
            String[] dyKeyword = kwLine.split("\t");

            CrawlerDomainKeywordCommand crawlerDomainKeywordCommand =
                    new CrawlerDomainKeywordCommand("192.168.1.215", 9599);

            CrawlerDomainKeys crawlerDomainKeys = new CrawlerDomainKeys();
            crawlerDomainKeys.setDomain("xhs");
            crawlerDomainKeys.setKeyword(dyKeyword[0]);
            crawlerDomainKeys.setSite(dyKeyword[1]);
            crawlerDomainKeys.setStatus(0);
            crawlerDomainKeywordCommand.addOrUpdateKeys(crawlerDomainKeys);
            System.out.println(JSON.toJSONString(dyKeyword));
        }
    }

    private static void localProfile() throws IOException {

        List<String> kwLines = IOUtils.readLines(new FileInputStream("E:\\chance_work\\chance-crawler-development\\crawler-scripts-command\\src\\main\\java\\com\\chance\\cc\\crawler\\development\\command\\schedule\\xhs\\xhs_kw.txt"), Charset.forName("UTF-8"));

        for (String kwLine : kwLines) {
            if (kwLine.startsWith("#")) {
                continue;
            }
            String[] dyKeyword = kwLine.split("\t");

            CrawlerDomainKeywordCommand crawlerDomainKeywordCommand =
                    new CrawlerDomainKeywordCommand("192.168.1.215", 9599);

            CrawlerDomainKeys crawlerDomainKeys = new CrawlerDomainKeys();
            crawlerDomainKeys.setDomain("xhs");

            Map<String, String> keywordJson = new HashMap<>();
            keywordJson.put("keyword", dyKeyword[0]);
            keywordJson.put("hours", dyKeyword[1]);
            keywordJson.put("startTime", dyKeyword[2]);
            keywordJson.put("endTime", dyKeyword[3]);

            crawlerDomainKeys.setKeyword(JSON.toJSONString(keywordJson));
            crawlerDomainKeys.setSite(dyKeyword[4]);
            crawlerDomainKeys.setStatus(0);
//            crawlerDomainKeywordCommand.addOrUpdateKeys(crawlerDomainKeys);
            crawlerDomainKeywordCommand.delKeys(crawlerDomainKeys);
            System.out.println(JSON.toJSONString(dyKeyword));
        }
    }

//    private static void processKW() throws IOException {
//
//
////        List<String> kwLines = IOUtils.readLines(new FileInputStream("D:\\chance\\gitspace\\chance-crawler-development\\crawler-scripts-command\\src\\main\\java\\com\\chance\\cc\\crawler\\development\\command\\schedule\\xhs\\xhs_kw.txt"), Charset.forName("UTF-8"));
//        List<String> kwLines = IOUtils.readLines(new FileInputStream("C:\\Users\\Administrator\\Desktop\\jar\\nike_new_kw.txt"), Charset.forName("UTF-8"));
//        List<String> results = new ArrayList<>();
//        int count = 0;
//        FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\jar\\nike_kw.txt");
//        int index = 1;
//        for (String kwLine : kwLines) {
//            if (count % 30 == 0){
////                for (String result : results) {
////                    bw.write(result);
////                    bw.write("\n");
////
////                }
//                IOUtils.writeLines(results,"\n",fileOutputStream,"utf-8");
//                IOUtils.write(  "----------------"+index+"---------------",fileOutputStream,"utf-8");
//                IOUtils.write("\n",fileOutputStream,"utf-8");
//                index ++;
//                results.clear();
//            }
//            results.add(kwLine);
//            count ++;
//        }
//
//        IOUtils.writeLines(results,"\n",fileOutputStream,"utf-8");
//    }
//
//    private static void processKW1() throws IOException {
//
//
//        List<String> kwLines = IOUtils.readLines(new FileInputStream("D:\\chance\\gitspace\\chance-crawler-development\\crawler-scripts-command\\src\\main\\java\\com\\chance\\cc\\crawler\\development\\command\\schedule\\xhs\\xhs_kw.txt"), Charset.forName("UTF-8"));
//        Set<String> kws = new HashSet<>(kwLines);
//        List<String> results = new ArrayList<>();
//        FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\jar\\nike_new_kw.txt");
//        List<String> delResults = new ArrayList<>();
//        for (String kwLine : kws) {
//            if (results.size() < 1){
//                results.add(kwLine);
//            }
//            boolean flag = false;
//            for (String result : results) {
//                if (kwLine.length() > result.length()){
//                    flag = kwLine.toLowerCase().contains(result.toLowerCase());
//                } else {
//                    flag = result.toLowerCase().contains(kwLine.toLowerCase());
//                    if (flag){
//                        flag = false;
//                        delResults.add(result);
//                    }
//                }
//                if (flag){
//                    break;
//                }
//            }
//
//            if (!flag){
//                results.add(kwLine);
//            }
//        }
//
//        for (String delResult : delResults) {
//            results.remove(delResult);
//        }
//
//        IOUtils.writeLines(results,"\n",fileOutputStream,"utf-8");
//    }
}
